package world.criver.imagecompresstool.application;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import world.criver.imagecompresstool.data.daValue;
import world.criver.imagecompresstool.function.funIcon;
import world.criver.imagecompresstool.function.funSize;
import world.criver.imagecompresstool.function.funThumb;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class appPop extends Application {

    private String[] result=null;
    private String mode;
    private long id;
    private double windows_w,windows_h;
    private int show_time=50*3;
    public appPop(long id,String mode,String[] result){
        this.result=result;
        this.id=id;
        this.mode=mode;
    }
    @Override
    public void start(Stage self) throws Exception {
        FXMLLoader loader=new FXMLLoader(appPop.this.getClass().getResource("/world/criver/imagecompresstool/fxml/pop.fxml"));
        Scene sce=new Scene(loader.load());
        sce.setFill(null);
        self.setScene(sce);
        self.setTitle("结果提示框");
        self.setAlwaysOnTop(true);

        updateInfo(self);

        Stage parent=new Stage();
        parent.setOpacity(0);
        parent.initStyle(StageStyle.UTILITY);
        self.initStyle(StageStyle.TRANSPARENT);
        self.initOwner(parent);

        daValue.pops.add(id);

        parent.show();
        self.show();

        Screen sc=Screen.getPrimary();
        windows_w=sc.getBounds().getWidth();
        windows_h=sc.getBounds().getHeight();
        self.setX(windows_w-224);
        self.setY(windows_h-114);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(() -> {
                    self.setY(windows_h - 114 - daValue.pops.indexOf(id) * 72);
                    if (show_time > 0) show_time--;
                    if (show_time <= 0) {
                        double o=self.getOpacity() - 0.05;
                        self.setOpacity(o>=0?o:0);
                        if (o > 0) return;
                        self.hide();
                        parent.hide();
                        daValue.pops.remove(id);
                    }
                });
            }
        },new Date(),20);
    }
    private void updateInfo(Stage self){
        String mo=mode.contains("compress")?"压缩":"转ICO";
        String le="";
        switch (mode){
            case funThumb.mode_1 -> le="普";
            case funThumb.mode_2 -> le="高";
            case funThumb.mode_3 -> le="低";
            case funIcon.mode_auto -> le="自";
            case funIcon.mode_16x16 -> le="16";
            case funIcon.mode_32x32 -> le="32";
            case funIcon.mode_48x48 -> le="48";
            case funIcon.mode_64x64 -> le="64";
            case funIcon.mode_128x128 -> le="128";
            case funIcon.mode_256x256 -> le="256";
            default -> le="无";
        }
        boolean ok=result[2].equals("true");
        long o=Long.parseLong(result[1]),n=Long.parseLong(result[4]);
        funSize funSize=new funSize();
        Label re=((Label)self.getScene().lookup("#result"));
        re.setText(ok?(mo+"成功"):(mo+"失败"));
        re.setTextFill(ok?new Color(0.1,1,0.09,1):new Color(1,0.65,0.65,1));
        ((Label)self.getScene().lookup("#filename")).setText(result[0]);
        ((Label)self.getScene().lookup("#level")).setText(le);
        ((Label)self.getScene().lookup("#old_size")).setText(funSize.numToStr(o));
        Label to=((Label)self.getScene().lookup("#to_im"));
        to.setText(ok?">":"x");
        to.setTextFill(ok?new Color(0.1,1,0.09,1):new Color(1,0.65,0.65,1));
        ((Label)self.getScene().lookup("#new_size")).setText(funSize.numToStr(n));
        ((Label)self.getScene().lookup("#pro")).setText(String.format("%.2f",(1-n*1.0/o)*100)+"%");
    }
}
